const moment = require('moment');

const APIError = require('../rest').APIError;

const db = require('../mysql');

async function getData(sql) {
    let dataList = await db(sql);
    return dataList
}

module.exports = {
    'GET /api/list': async(ctx,next)=>{     //文章列表
        let data = await getData('select title,id,creatTime from data')
        if(data){
            ctx.rest(data);
        }else {
            throw new APIError('product:not_found', 'product not found by id.');
        }
    },
    'GET /api/information/:id': async(ctx,next)=>{  //文章详细信息
        console.log(ctx.params)
        let data = await getData(`select * from data where id=${ctx.params.id}`)
        if(data){
            ctx.rest(data);
        }else {
            throw new APIError('product:not_found', 'product not found by id.');
        }
    },
    'POST /api/addType': async(ctx,next)=>{  //添加分类
        console.log(ctx.request.body.name)
        let sql=`INSERT INTO type values(null,'${ctx.request.body.name}')`
        console.log(sql)
        let data=await getData(sql);
        if(data){
            ctx.rest({status:'OK'});
        }else {
            throw new APIError('product:not_found', 'product not found by id.');
        }
    },
    'GET /api/typeList': async(ctx,next)=>{  //分类列表
        let sql = `SELECT t_id as id, t_name as name FROM type`
        let data=await getData(sql)
        if(data){
            ctx.rest(data);
        }else {
            throw new APIError('product:not_found', 'product not found by id.');
        }
    },
    'POST /api/deleteType': async(ctx,next)=>{  //删除分类
        let sql = `DELETE FROM type WHERE t_id = '${ctx.request.body.id}'`;
        let data = await getData(sql)
        if(data){
            ctx.rest(data);
        }else {
            throw  new APIError('errror')
        }
    },
    'POST /api/modifyTypeName': async(ctx,next)=>{
        let sql = `UPDATE type SET t_name = '${ctx.request.body.name}' WHERE t_id = '${ctx.request.body.id}'`;
        let data = await getData(sql)
        if(data){
            ctx.rest(data);
        }else {
            throw new APIError('product:not_found', 'product not found by id.');
        }
    },

    'GET /api/getTypeNote/:id': async(ctx,next)=>{  //获取笔记分类列表
        let sql = `SELECT * FROM data WHERE typeId='${ctx.params.id}'`;
        let data = await getData(sql)
        if(data){
            ctx.rest(data)
        }else {
            throw new APIError('error')
        }
    },
    'POST /api/addWrite': async(ctx,next)=>{  //添加文章
        let sql=`INSERT INTO data values(null,'${ctx.request.body.title}','${ctx.request.body.content}','OK','${moment().format('YYYY-MM-DD')}','${ctx.request.body.typeId}')`
        console.log(sql)
        let data=await getData(sql);
        if(data){
            ctx.rest({status:'OK'});
        }else {
            throw new APIError('ERROR');
        }
    },
    'POST /api/deleteWrite': async(ctx,next)=>{  //删除文章
        let sql =`DELETE FROM data WHERE id = '${ctx.request.body.id}'`
        let data=await getData(sql);
        if(data){
            ctx.rest({status:'OK'});
        }else {
            throw new APIError('ERROR');
        }
    },
    'POST /api/modifyWrite': async(ctx,next)=>{
        let sql = `UPDATE data SET title='${ctx.request.body.title}', content = '${ctx.request.body.content}' WHERE id = '${ctx.request.body.id}'`;
        let data = await getData(sql);
        if(data){
            ctx.rest({status:'OK'})
        }else {
            throw new APIError('ERROR')
        }
    },
    'GET /api/login':async(ctx,next)=>{
        let sql =`SELECT * FROM user WHERE passWord='${ctx.query.password}' AND userName='${ctx.query.name}'`
        console.log(sql)
        let data=await getData(sql);
        console.log(data.length)
        if(data.length>0){
            ctx.redirect('/write');
            ctx.rest({status:'OK'})
        }else {
            ctx.redirect('/')
            ctx.rest({status:'error'})
        }
    }

};
